#!/usr/bin/env ruby
#encoding: utf-8

require 'pp'

class Quiz
  class << self
    # IQ Test
    # Bob is preparing to pass IQ test.
    # The most frequent task in this test is to find out which one of the given numbers differs from the others.
    # Bob observed that one number usually differs from the others in evenness.
    # Help Bob — to check his answers, he needs a program that among the given numbers finds one that is different in evenness,
    #   and return a position of this number.
    #
    # ! Keep in mind that your task is to help Bob solve a real IQ test, which means indexes of the elements start from 1 (not 0)
    # Examples :
    #    iq_test("2 4 7 8 10") => 3 // Third number is odd, while the rest of the numbers are even
    #    iq_test("1 2 1 1") => 2 // Second number is even, while the rest of the numbers are odd
    # ----------------------------------------------------------------------------------------------------------------
    # @param String numbers
    # @return Integer position of the number
    def iq_test(numbers)
      list = numbers.to_s.split(' ').map! {|x| x.to_i }
      first_is_even = list[0] % 2 === 0
      # find index of different as first number
      result_index = list.index { |x| (x % 2 == 0) != first_is_even }
      if result_index === 1
        third_is_even = (list[2] % 2 === 0)
        return result_index + 1 if first_is_even === third_is_even
        1
      else
        result_index + 1
      end
    end

    # Who likes it
    # -------------------
    # You probably know the "like" system from Facebook and other pages. People can "like" blog posts,
    # pictures or other items. We want to create the text that should be displayed next to such an item.
    # Implement a function likes :: [String] -> String, which must take in input array,
    # containing the names of people who like an item. It must return the display text as shown in the
    #
    # examples:
    # likes [] // must be "no one likes this"
    # likes ["Peter"] // must be "Peter likes this"
    # likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
    # likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
    # likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"
    # ---------------------------------------------------------------------------------------------------------------
    def likes(names)
      case names.size
        when 0
          'no one likes this'
        when 1
          "#{names.first} likes this"
        when 2
          "#{names[0]} and #{names[1]} like this"
        when 3
          "#{names[0]}, #{names[1]} and #{names[2]} like this"
        else
          "#{names[0]}, #{names[1]} and #{names.size - 2} others like this"
      end
    end

    # Open or Senior
    # Input
    #
    # Input will consist of a list of lists containing two items each. Each list contains information
    # for a single potential member. Information consists of an integer for the person's age
    # and an integer for the person's handicap.
    #
    #    Example Input
    #
    #    [[18, 20],[45, 2],[61, 12],[37, 6],[21, 21],[78, 9]]
    # Output
    #
    # Output will consist of a list of string values (in Haskell: Open or Senior) stating
    # whether the respective member is to be placed in the senior or open category.
    #
    #    Example Output
    #
    #    ["Open", "Open", "Senior", "Open", "Open", "Senior"]
    #
    def openOrSenior(data)
      data.map {|person|
        if person[0] >= 55 && person[1] > 7
          'Senior'
        else
          'Open'
        end
      }
    end
  end

end